<template>
    <el-dialog custom-class="form-dialog" :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false" width="800px" v-loading="loading"
        :before-close="handleClose">
        <!-- 表单内容 -->
        <div class="form-dialog-content-box">
            <el-form :model="form" label-width="100px" :rules="rules" ref="ruleForm" :size="size">
                <el-form-item label="父级：" prop="pid">
                    <el-cascader :size="size" v-model="form.pid" :disabled="title != '新增'"
                        :options="treeTableData" clearable
                        :props="{ checkStrictly: true, value: 'id', label:'roleName' }">
                    </el-cascader>
                </el-form-item>
                <el-form-item label="角色名：" prop="roleName">
                    <el-input v-model="form.roleName" :size="size"></el-input>
                </el-form-item>
                <el-form-item label="描述：" prop="description" style="width: 100%;">
                    <el-input v-model="form.description" :size="size" type="textarea" :rows="3"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <!-- 提交区域 -->
        <span slot="footer" class="dialog-footer">
            <el-button @click="handleCancelBtn" :size="size">取 消</el-button>
            <el-button type="primary" @click="handleSubmitBtn" :size="size">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
//引入混入逻辑
import mixin from '@/mixins/form'
export default {
    name: "formDialog",
    mixins: [mixin],
    props: {
        title: {
            type: String,
            default: "新增"
        },
        treeTableData: {
            type: Array,
            default: function(){
                return []
            }
        }
    },
    data() {
        return {
            loading: false,//页面加载状态

            dialogVisible: false,//对话框展示状态
            form: {
                id: "",
                pid: "",//父级id
                roleName: "",
                description: "",
            },//表单数据

            rules: {
                pid: [
                    { required: true, message: '请选择父级', trigger: 'change' },
                ],
                roleName: [
                    { required: true, message: '请输入角色名', trigger: 'blur' },
                ],
            },//表单验证规则
        }
    },
    created(){
        //管理员可以创建顶级角色，可以取消限制
        if(this.userInfo.isAdmin == 1){
            this.rules.pid[0].required = false
        }
    },
    methods: {
        //重写 处理确认按钮的事件
        handleSubmitBtn() {
            //执行校验
            this.$refs.ruleForm.validate((valid) => {
                //判断校验结果
                if (valid) {
                    //校验成功
                    //深拷贝
                    var form = this.$utils.deepCopy(this.form)
                    //取数组最后一个元素
                    if(Array.isArray(form.pid)){
                        form.pid = form.pid.pop()
                    }
                    //事件抛出
                    this.$emit('submit', form)
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        }
    },
    computed: {
        ...mapState(['userInfo']),
    }
}
</script>

<style scoped lang="scss">
.form-dialog-content-box {
    ::v-deep .el-form{
        display: flex;
        flex-wrap: wrap;
        .el-form-item{
            width: 50%;
            .el-cascader{
                display: block;
            }
        }
    }
}
</style>
